import * as NS from "../../Common/NetServices";
import { Wx_XSPPage } from "../../Common/Entitys";
import { IsNull, IsNullOrWhiteSpace } from "../../TYLibrary/Common/GlobalExtend";
import { Config } from "../../Config";
import { Thread } from "../../TYLibrary/System/System";
import { Wx_PageData, WXDataClassRuntimeDecorator } from "../../Common/EntityBase";
import { List } from "../../TYLibrary/System/Collections";

@WXDataClassRuntimeDecorator()
class UserInfoPageData extends Wx_PageData {
    public UserInfo = {};
    public SexArr = ["女", "男", "不限"];
    public index: number = 0;
    public IsShowName: boolean = false;
    public UserArea = [];
    public IsShowIntroductions: boolean = false;
    public IsShow: boolean = false;
}

class UserInfoPage extends Wx_XSPPage {
    private m_BIRTHDAY;
    private m_ImgURL: string = "";
    public Data: UserInfoPageData = new UserInfoPageData();

    /**
     * 初始化页面信息
     * @param {*} options
     * @returns {Promise<void>}
     * @memberof UserInfoPage
     */
    public async InitData(options: any): Promise<void> {
        // try {
        //     this.m_ImgURL = options.img_url;
        //     wx.showLoading({
        //         title: "数据加载中.....",
        //         icon: "loading"
        //     });
        //     if (this.m_ImgURL) {
        //         this.App.UserInfo.AVATARURL = this.m_ImgURL;
        //         await this.UpLoadImg(this.m_ImgURL);
        //         await this.UpdateUser();
        //     }
        //     if (!IsNull(this.App.UserInfo.BIRTHDAY as any))
        //         (this.App.UserInfo.BIRTHDAY as any) = TimestampToTime(this.App.UserInfo.BIRTHDAY);
        //     if (!IsNull(this.App.UserInfo.AREA)) {
        //         this._SetData({ UserArea: this.App.UserInfo.AREA.split(",") });
        //     }
        //     await this._SetData({ UserInfo: this.App.UserInfo });
        //     await this._SetData({ IsShow: true });
        //     wx.hideLoading();
        // } catch (e) {
        //     this.ShowError(e);
        // } finally {
        //     wx.hideLoading();
        // }
    }

    /**
     *
     * @returns {Promise<void>}
     * @memberof UserInfoPage
     */
    public async onShowAgain(): Promise<void> {
        // try {
        //     if (this.m_ImgURL) await this.UpLoadImg(this.m_ImgURL);
        //     await this.UpdateUser();
        // } catch (e) {
        //     this.ShowError(e);
        // }
    }

    /**
     * 选择图片
     * @param {*} e
     * @returns {Promise<void>}
     * @memberof UserInfoPage
     */
    public async chooseImageHandler(e: any): Promise<void> {
        wx.navigateTo({ url: "../cutInside/cutInside?type=1" });
    }

    /**
     * 上传图片信息
     * @param {*} imgUrl
     * @returns {Promise<void>}
     * @memberof UserInfoPage
     */
    public async UpLoadImg(imgUrl: any): Promise<void> {
        // try {
        //     wx.showLoading({
        //         title: "图片上传中.....",
        //         icon: "loading"
        //     });
        //     wx.uploadFile({
        //         url: Config.UpLoadFile, //仅为示例，非真实的接口地址
        //         filePath: imgUrl,
        //         header: {
        //             "content-type": "application/json",
        //             EncryptStr: wx.getStorageSync("EncryptStr") || "",
        //             UnionId: wx.getStorageSync("UnionId") || ""
        //         },
        //         name: "file",
        //         formData: {
        //             Folder: "uploadFile/User"
        //         },
        //         success: async res => {
        //             let data: any = res.data;
        //             this.App.UserInfo.AVATARURL = JSON.parse(data).RESULT;
        //             await this.UpdateUser();
        //             wx.hideLoading();
        //         },
        //         fail: e => {
        //             this.ShowError(e);
        //         }
        //     });
        // } catch (e) {
        //     this.ShowError(e);
        // } finally {
        //     wx.hideLoading();
        // }
    }

    /**
     *
     * @returns {Promise<void>}
     * @memberof UserInfoPage
     */
    public async UpdateUser(): Promise<void> {
        // try {
        //     this.data.UserInfo = this.App.UserInfo;
        //     this._SetData(this.data);
        //     await NS.UpdateUserInfo(this.App.UserInfo);
        // } catch (e) {
        //     this.ShowError(e);
        // }
    }

    /**
     *
     * @param {*} e
     * @memberof UserInfoPage
     */
    public NameHandler(e: any): void {
        this.Data.IsShowName = true;
        this.Data.ApplyChange(this);
    }

    /**
     *
     * @param {*} e
     * @memberof UserInfoPage
     */
    public bindIntroductions(e: any): void {
        this.Data.IsShowIntroductions = true;
        this.Data.ApplyChange(this);
    }

    /**
     *
     * @param {*} e
     * @memberof UserInfoPage
     */
    public ConfrimNameHandler(e: any): void {
        // console.log("名称",this.App.UserInfo.USERNAME);
        // if (!IsNullOrWhiteSpace(this.App.UserInfo.USERNAME)) {
        //     this.UpdateUser();
        //     this.Cancel();
        // } else this.ShowError("名称不能为空!");
    }

    /**
     *
     * @memberof UserInfoPage
     */
    public ConfrimIntroductions() {
        // console.log("start");
        // if (!IsNullOrWhiteSpace(this.App.UserInfo.INTRODUCTIONS)) {
        //     this.UpdateUser();
        //     this.Cancel();
        //     console.log("end");
        // } else this.ShowError("介绍内容不能为空！");
    }

    /**
     *
     * @param {*} e
     * @memberof UserInfoPage
     */
    public InputNameHandler(e: any): void {
        // this.App.UserInfo.USERNAME = e.detail.value;
    }

    /**
     *
     * @param {*} e
     * @memberof UserInfoPage
     */
    public bindInputIntroductions(e: any): void {
        // this.App.UserInfo.INTRODUCTIONS = e.detail.value;
    }

    /**
     * 取消
     * @memberof UserInfoPage
     */
    public CancelHandler(): void {
        this.Data.IsShowName = false;
        this.Data.IsShowIntroductions = false;
        this.Data.ApplyChange(this);
    }

    /**
     * 更新性别
     * @param {*} e
     * @memberof UserInfoPage
     */
    public SexHandler(e: any): void {
        wx.showActionSheet({
            itemList: this.Data.SexArr,
            success: res => {
                // this.App.UserInfo.SEX = res.tapIndex + 1;
                // this.UpdateUser();
            },
            fail: e => {
                console.log("用户取消更改性别信息");
            }
        });
    }

    /**
     * 更新出生年月
     * @param {*} e
     * @returns {Promise<void>}
     * @memberof UserInfoPage
     */
    public async DateChangeHandler(e: any): Promise<void> {
        // this.App.UserInfo.BIRTHDAY = new Date(e.detail.value).getTime();
        // await NS.UpdateUserInfo(this.App.UserInfo);
        // this.App.UserInfo.BIRTHDAY = e.detail.value;
        // this.m_BIRTHDAY = this.App.UserInfo.BIRTHDAY;
        // this._SetData({ UserInfo: this.App.UserInfo });
    }

    /**
     * 更新地区
     * @param {*} e
     * @memberof UserInfoPage
     */
    public bindRegionChange(e: any): void {
        // let v: Array<any> = e.detail.value;
        // let area = v.join(",");
        // console.log(area);
        // // this.App.UserInfo.AREA = area;
        // this.UpdateUser();
    }
}

Page(new UserInfoPage());
